import httplib2

from google.appengine.api import users
from oauth2client.appengine import StorageByKeyName, CredentialsModel
from oauth2client.client import AccessTokenRefreshError


def get_current_user_credentials():
    """
    Fetch the current current user's credentials, refreshing them
    if needed.
    """
    user = users.get_current_user()
    if not user:
        return None

    storage = StorageByKeyName(CredentialsModel, user.user_id(), 'credentials')
    credentials = storage.get()

    if not credentials or not credentials.access_token:
        return None

    if credentials.access_token_expired:
        http = httplib2.Http()
        try:
            credentials.refresh(http)
        except AccessTokenRefreshError:
            return None
        storage.put(credentials)

    return credentials
